local idle_text = {}
local idle_max = 200
for i = 1, idle_max do
    local panel = class.panel.create('',0,0,25,25)
    panel:set_level(30)
    panel.fonts = {}
    for a=1,7 do
        panel.fonts[a] = panel:add_panel('',(a-1)*25*0.55,0,25,25)
    end
    idle_text[i] = panel
end


local texttag = {}
ac.ui_tag2 = texttag
setmetatable(texttag, texttag)
texttag.group = {}

-- 结构
local mt = {}
texttag.__index = mt

-- 类型
mt.type = 'ui tag'

-- 句柄
mt.handle = nil

-- 文本内容
mt.string = '无文本'

-- 缩放
mt.scale = 1
mt.font_size = 12

-- 初始位置
mt.position = ac.point(0, 0)

-- 速度
mt.speed = 0

-- 角度
mt.angle = 90

-- 颜色
mt.color = 0xffffffff
mt.alpha = 1

mt.clock = 0

-- 生命周期
mt.life = 3

-- 淡化
mt.fade = 2

-- 可见性
mt.show = true

-- 设置文本
local paths = {
    ['物理'] = {
        ['b'] = [[ui\漂浮文字\红暴击.tga]],
        ['w'] = [[ui\漂浮文字\红字\万.tga]],
        ['y'] = [[ui\漂浮文字\红字\亿.tga]],
        ['0'] = [[ui\漂浮文字\红字\0.tga]],
        ['1'] = [[ui\漂浮文字\红字\1.tga]],
        ['2'] = [[ui\漂浮文字\红字\2.tga]],
        ['3'] = [[ui\漂浮文字\红字\3.tga]],
        ['4'] = [[ui\漂浮文字\红字\4.tga]],
        ['5'] = [[ui\漂浮文字\红字\5.tga]],
        ['6'] = [[ui\漂浮文字\红字\6.tga]],
        ['7'] = [[ui\漂浮文字\红字\7.tga]],
        ['8'] = [[ui\漂浮文字\红字\8.tga]],
        ['9'] = [[ui\漂浮文字\红字\9.tga]],
    },
    ['魔法'] = {
        ['b'] = [[ui\漂浮文字\蓝暴击.tga]],
        ['w'] = [[ui\漂浮文字\蓝字\万.tga]],
        ['y'] = [[ui\漂浮文字\蓝字\亿.tga]],
        ['0'] = [[ui\漂浮文字\蓝字\0.tga]],
        ['1'] = [[ui\漂浮文字\蓝字\1.tga]],
        ['2'] = [[ui\漂浮文字\蓝字\2.tga]],
        ['3'] = [[ui\漂浮文字\蓝字\3.tga]],
        ['4'] = [[ui\漂浮文字\蓝字\4.tga]],
        ['5'] = [[ui\漂浮文字\蓝字\5.tga]],
        ['6'] = [[ui\漂浮文字\蓝字\6.tga]],
        ['7'] = [[ui\漂浮文字\蓝字\7.tga]],
        ['8'] = [[ui\漂浮文字\蓝字\8.tga]],
        ['9'] = [[ui\漂浮文字\蓝字\9.tga]],
    },
}
function mt:set_text(string)
	if string then
		self.string = string
    end

    if self.handle then
        local dmgTy = self.dmgTy or '物理'
        local str = tostring(self.string)
        self.w = 0
        for a,panel in ipairs(self.handle.fonts) do
            local key = str:sub(a,a)
            local path = paths[dmgTy] or paths['物理']
            local icon = path[key]
            if icon then
                panel:set_normal_image(icon)
                self.w = self.w + panel.w*0.7
            else
                panel:set_normal_image('')
            end
        end
    end
end

-- 设置大小
function mt:set_scale(scale)
	if scale then
		self.scale = scale
    end
	self.handle:set_relative_size(scale or self.scale)
end

function mt:add_scale(diff)
	self.scale = self.scale + diff
	self.handle:set_relative_size(self.scale)
end

-- 设置位置
mt.offset_x = 0
mt.offset_y = 0
function mt:set_position(position)
	if not self.show then
		return
	end
	if position then
		self.target = position
	end
    local point = self.target:get_point()
	local x, y, z = point:get()
	
    local sx, sy = ac.frame.w2s(x, y, z + point:get_height())

    if sx == nil or sx < 0 or sy < 32 or sx > 1920 or sy > 768 then
        self.handle:hide()
    else
        local w = self.w
        self.handle:set_real_position(sx - w/2 + self.offset_x,sy - self.offset_y)
        self.handle:show()
    end
end

-- 设置颜色
function mt:set_color(color)
    --self.handle:set_color(color or self.color)
end

-- 透明度
function mt:set_alpha(alpha)
	self.handle:set_alpha(alpha or self.alpha)
end

-- 创建漂浮文字
function texttag:__call(texttag)
    setmetatable(texttag, self)
    
    local handle = table.remove(idle_text,1)
    if not handle then
        texttag.removed = true
        return texttag
	end
	
	local level = idle_max - #idle_text
    handle:hide()
    texttag.handle = handle
	texttag.handle:set_alpha(1)
	texttag:set_text()
	texttag:set_scale()
	texttag:set_position()
	texttag:set_color()
    
	
    self.group[texttag] = true
	
	return texttag
end

-- 移除漂浮文字
function mt:remove()
	if self.removed then
		return
    end
    
	self.removed = true
    
	self.handle:hide()
	self.handle:unbind_world()
	
	table.insert(idle_text, self.handle)
	
	texttag.group[self] = nil
	
	
end

mt.angle = 90
mt.speed = 0
function mt:move(angle,speed)
    self.angle = angle or self.angle
    self.speed = speed or self.speed
end

mt.jump_rate = 0.5
mt.jump_height = 25
function mt:update(delta)
	self.clock = self.clock + delta
	if self.clock > self.life - self.fade then
		if self.clock > self.life  then
			self:remove()
			return
		else
            local rate = 1 - (self.clock - self.fade)/(self.life - self.fade)
			self.alpha = rate
			self:set_alpha()
		end
	end
    if self.life>0 and self.jump_time then
        local time = self.clock
        if time<=self.jump_time then
            local rate = math.sin(time/self.jump_time * 180)
            local size = rate * self.jump_rate
            self.init_size = self.init_size or self.scale
            self:set_scale(self.init_size * (1 + size))
        else
            -- local size = (time-self.jump_time)/(self.life - self.jump_time)
            -- self.init_size = self.init_size or self.scale
            -- self:set_scale(self.init_size * (1 - size*0.5))
        end
    end
    if self.speed~=0 then
        self.offset_x = self.offset_x + delta*self.speed * math.cos(self.angle)
        self.offset_y = self.offset_y + delta*self.speed * math.sin(self.angle)
    end
    -- local x,y,z = self.position:get()
	-- if self.jump > 0 then
	-- 	if self.clock > self.jump then
	-- 		y = y - self.speed / (delta * 1000) / 10
	-- 	else
	-- 		y = y + self.speed / (delta * 1000)
	-- 	end
	-- 	local value = self.jump_max * math.sin(math.max(self.clock,0) / self.life * 180) + self.jump_min
    --     self:set_scale(value)
	-- 	x = x + self.x_speed / (delta * 1000)
	-- else
	-- 	x = x + self.speed * math.cos(self.angle)*delta
	-- 	y = y + self.speed * math.sin(self.angle)*delta
    --     self:set_scale()
	-- end
	-- self.position = ac.point(x,y)
	
	self:set_position()
end


ac.hardware:on("界面绘制")(function(_,delta)
    for tt in pairs(texttag.group) do
        tt:update(delta)
    end
end)

return texttag

